package com.zhx.demo.controller;

import com.ygsoft.activiti.service.impl.RespositoryServiceImpl;
import com.zhx.demo.entity.Leave;
import com.zhx.demo.entity.User;
import com.zhx.demo.service.impl.LeaveServiceImpl;
import com.zhx.demo.utils.SpringSessionUtil;
import net.sf.json.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.File;

/**
 * 请假流程
 *
 * @author zhx
 * @create 2017-11-15 9:36
 **/
@Controller
public class LeaveController {

    @Autowired
    private LeaveServiceImpl leaveService;

    @Autowired
    private RespositoryServiceImpl respositoryService;


    private  final static Logger logger = LoggerFactory.getLogger(LeaveController.class);

    /**
     * @Date: 14:18 2017/6/16
     * 获取所有流程定义
     */
    @RequestMapping(value = "/getMyLeave",method = RequestMethod.GET)
    @ResponseBody
    public JSONArray getMyLeave(@RequestParam("userId") String userId,HttpSession session){
        User tmpUser = SpringSessionUtil.getSession(session);

        if(StringUtils.isBlank(userId)){
            return JSONArray.fromObject(leaveService.getLeaveList(""));
        }else{
            return JSONArray.fromObject(leaveService.getLeaveList(Long.toString(tmpUser.getId())));
        }

    }

    @PostMapping("/startleave")
    @ResponseBody
    public String srartLeave(@RequestParam(value = "reason") String reason,
                             @RequestParam(value = "start_time") String start_time,
                             @RequestParam(value = "end_time") String end_time,
                             @RequestParam(value = "leave_type") String leave_type,HttpSession session){


        Leave leave = new Leave();
        leave.setEnd_time(end_time);
        leave.setStart_time(start_time);
        leave.setReason(reason);
        leave.setApply_time(Long.toString(System.currentTimeMillis()));
        leave.setLeave_type(leave_type);


        logger.info("收到的请假表单:{}",leave);
        User tmpUser = SpringSessionUtil.getSession(session);

        if(tmpUser == null){
            return "";
        }

        leave.setUserId(Long.toString(tmpUser.getId()));

        boolean b = leaveService.startLeave(leave, Long.toString(tmpUser.getId()));
        if(b){
            return "success";
        }else{
            return "fail";
        }
    }

    /**
     * @Date: 14:23 2017/6/16
     * 获取指定流程定义XML
     * @param request 前端传回请求
     */
    @RequestMapping(value = "/getLeaveImg",method = RequestMethod.GET)
    @ResponseBody
    public String getLeaveImg(HttpServletRequest request,HttpSession session) throws Exception{
        String processId = request.getParameter("processId");
        //对流程id进行转码
        processId = new String(processId.getBytes("ISO-8859-1"),"UTF-8");

        BufferedImage progressImg = respositoryService.getProgressImg(processId);

        try {
            String fileName = "";
            String fileName_url = "";
            User tmpUser = SpringSessionUtil.getSession(session);
            if(tmpUser!=null) {
                String rootContext = System.getProperty("web.root");
                fileName = rootContext+ "static/process_images/"+tmpUser.getUsername()+ "-" + "his-" + processId + ".png";
                System.out.println(fileName);
                File file = new File(fileName);
                ImageIO.write(progressImg, "PNG", file);
                fileName_url = request.getContextPath() + "/static/process_images/" + tmpUser.getUsername()+ "-" + "his-" + processId + ".png";
            }

            return fileName_url;
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }







}
